﻿using DropBoxClient;
using LiteBlog.Common.Components;
using LiteBlog.Common.Enums;
using LiteBlog.Components.Contracts;
using LiteBlog.Components.Enums;
using OAuth2Authorizer;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Net;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Web;

namespace LiteBlog.Components
{
    public class BackupComp : IBackupComp
    {
        private IAuthorizer _authorizer = null;
        private ISettingsComp _settingsComp;
        private IConfigComp _configComp;

        public ISettingsComp SettingsComponent
        {
            get
            {
                return _settingsComp;
            }
        }

        public IConfigComp ConfigComponent
        {
            get
            {
                return _configComp;
            }
        }

        public BackupComp(ISettingsComp settingsComp, IConfigComp configComp)
        {
            _authorizer = AuthorizerBuilder.Create(BuiltInAuthorizers.DropBox);
            _settingsComp = settingsComp;
            _configComp = configComp;
        }

        public MethodResult Backup()
        {
            var settings = SettingsComponent.Load();
            var accessToken = settings.AccessToken;
            string timestamp = DateTime.Now.ToString("ddMMyyyyhhmmss");
            string appdataArchive = string.Format("AppData.{0}.zip", timestamp);
            string documentArchive = string.Format("Documents.{0}.zip", timestamp);
            BackupFolder(accessToken, ConfigComponent.DataPath, appdataArchive);
            BackupFolder(accessToken, ConfigComponent.DocumentPath, documentArchive);
            return MethodResult.BackupSuccess;
        }

        private void BackupFolder(string accessToken, string folderName, string zipName)
        {
            Logger.Log(string.Format("Starting to backup {0} ...", folderName));
            string backupFolder = ConfigComponent.BackupPath;
            string archiveName = Path.Combine(backupFolder, zipName);
            System.IO.Compression.ZipFile.CreateFromDirectory(folderName, archiveName);

            using (var reader = new StreamReader(archiveName))
            {
                Logger.Log("Hitting dropbox now");
                var client = new OAuth2Client(accessToken);
                client.PutFiles("sandbox", zipName, reader.BaseStream);
            }
            Logger.Log(string.Format("Backedup {0}", folderName));
        }

        public string GetRegisterUrl(string redirectUrl, string state)
        {
            var settings = SettingsComponent.Load();
            return _authorizer.GetAuthorizeUrl(settings.AppKey, redirectUrl, state);
        }

        public void GetAndSaveAccessToken(string redirectUrl, string code)
        {
            var settings = SettingsComponent.Load();
            var accessToken = _authorizer.GetAccessToken(settings.AppKey, settings.AppSecret, redirectUrl, code);
            SettingsComponent.Update(SettingType.AccessToken, accessToken.AccessToken);
        }
    }
}
